package com.szxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szxy.constants.Constants;
import com.szxy.domain.DictData;
import com.szxy.dto.DictDataDto;
import com.szxy.mapper.DictDataMapper;
import com.szxy.service.DictDataService;
import com.szxy.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: zwz
 * @Date:2020/11/7 20:36
 * @Description:
 **/
@Service
public class DictDataServiceImpl implements DictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public DataGridView listPage(DictDataDto dictDataDto) {
        Page<DictData> page = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        qw.eq(StringUtils.isNotBlank(dictDataDto.getDictType()), DictData.COL_DICT_TYPE, dictDataDto.getDictType());
        qw.like(StringUtils.isNotBlank(dictDataDto.getDictLabel()), DictData.COL_DICT_LABEL, dictDataDto.getDictLabel());
        qw.eq(StringUtils.isNotBlank(dictDataDto.getStatus()), DictData.COL_STATUS, dictDataDto.getStatus());
        dictDataMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public int insert(DictDataDto dictDataDto) {
        DictData dictData = new DictData();
        // copy 属性
        BeanUtil.copyProperties(dictDataDto, dictData);
        // 设置创建者
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        dictData.setCreateTime(new Date());
        return dictDataMapper.insert(dictData);
    }

    @Override
    public int update(DictDataDto dictDataDto) {
        DictData dictData = new DictData();
        // copy 属性
        BeanUtil.copyProperties(dictDataDto, dictData);
        // 设置更新者
        dictData.setUpdateBy(dictDataDto.getSimpleUser().getUserName());
        return dictDataMapper.updateById(dictData);
    }

    @Override
    public int deleteDictDataByIds(Long[] dictCodeIds) {
        List<Long> dictCodeIdList = Arrays.asList(dictCodeIds);
        if (null != dictCodeIdList && dictCodeIdList.size() > 0) {
            dictDataMapper.deleteBatchIds(dictCodeIdList);
        }
        return -1;
    }

    /**
     * 从 redis 缓存中获取
     *
     * @param dictType
     * @return
     */
    @Override
    public List<DictData> selectDictDataByDictType(String dictType) {
        // 直接取缓存
        String json = this.redisTemplate.opsForValue().get(Constants.DICT_REDIS_PROFIX + dictType);
        return JSON.parseArray(json, DictData.class);
    }

    @Override
    public DictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectById(dictCode);
    }
}
